package com.example.backend.api.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.example.backend.api.common.Result;
import com.example.backend.api.service.impl.EmployeeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.text.ParseException;
import java.util.Date;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xwyzsn
 * @since 2023-03-07
 */
@RestController
@RequestMapping("/api/employee")
@CrossOrigin(origins = "*", maxAge = 3600)
public class EmployeeController {

    @Autowired
    private EmployeeServiceImpl employeeService;

    @GetMapping("/getall")
    public Result getAll(@RequestParam(value = "dept_id", required = false) Integer deptId) {
        return employeeService.getAll(deptId);
    }

    @PostMapping("/update")
    public Result update(@RequestParam(value = "user_id", required = true) Integer userId,
                         @RequestParam(value = "user_name", required = false) String userName,
                         @RequestParam(value = "birth", required = false) Date birth,
                         @RequestParam(value = "tel", required = false) String tel,
                         @RequestParam(value = "email", required = false) String email,
                         @RequestParam(value = "dept_id", required = false) Integer deptId,
                         @RequestParam(value = "gender", required = false) String gender,
                         @RequestParam(value = "address", required = false) String address,
                         @RequestParam(value = "password", required = false) String password,
                         @RequestParam(value = "nation", required = false) String nation,
                         @RequestParam(value = "education", required = false) String education,
                         @RequestParam(value = "qq", required = false) String qq
    ) {
        return employeeService.updateUser(userId, userName, birth, tel, email, deptId, gender, address, password, nation, education, qq);
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam(value = "user_id", required = true) Integer userId) {
        return employeeService.deleteUser(userId);
    }

    @PostMapping("/login")
    public Result login(@RequestParam(value = "username", required = true) Integer userId
                        , @RequestParam(value = "password", required = true) String password) {
        return employeeService.login(userId, password);
    }

    @GetMapping("getemployeebydept")
    public Result getEmployeeByDept(@RequestParam(value = "dept_id", required = true) Integer deptId) {
        return employeeService.getEmployeeByDept(deptId);
    }

    @PostMapping("add")
    public Result addEmploy(@RequestParam(value = "name",required = false) String username,
                            @RequestParam(value = "user_id") Integer userId,
                            @RequestParam(value = "birth",required = false) String birth,
                            @RequestParam(value = "dept_id",required = false) Integer deptId,
                            @RequestParam(value = "tel",required = false) String tel,
                            @RequestParam(value = "qq",required = false) String qq,
                            @RequestParam(value = "email",required = false) String email,
                            @RequestParam(value = "gender",required = false) String gender,
                            @RequestParam(value = "address",required = false) String address,
                            @RequestParam(value = "password",required = false) String password,
                            @RequestParam(value = "nation",required = false) String nation,
                            @RequestParam(value = "education",required = false) String education
        ) throws ParseException {
        return employeeService.addEmploy(username,birth,deptId,tel,qq,email,gender,address,password,nation,education,userId);
    }

}
